package java44th.group.controller.admin.order;


import java44th.group.bean.admin.bo.order.OrderRefundBO;
import java44th.group.bean.admin.bo.order.OrderShipBO;
import java44th.group.bean.bo.order.OrderReplyBO;
import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.admin.vo.order.CourierVO;
import java44th.group.bean.admin.vo.order.OrderDetailVO;
import java44th.group.bean.admin.vo.order.OrderListVO;
import java44th.group.service.admin.order.OrderService;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 小焦
 * @since 2022/10/11 20:35
 */

@RestController
@RequestMapping("admin/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @GetMapping("list")
    public BaseRespVo getOrderList(BasePageInfo pageInfo, Integer[] orderStatusArray, String orderId, @DateTimeFormat(pattern = "yy-MM-dd HH:mm:ss") Date start, @DateTimeFormat(pattern = "yy-MM-dd HH:mm:ss") Date end, String userId, String orderSn) {
        Integer intOrderId = null;
        if (!StringUtil.isEmpty(orderId)) {
            try {
                intOrderId = Integer.parseInt(orderId);
            } catch (NumberFormatException e) {
                return BaseRespVo.error(402, "订单Id必须为数字");
            }
        }
        Integer intUserId=null;
        if (!StringUtil.isEmpty(userId)) {
            try {
                 intUserId = Integer.parseInt(userId);
            } catch (NumberFormatException e) {
                return BaseRespVo.error(402, "用户Id必须为数字");
            }
        }

        CommonData<OrderListVO> orderCommonData = null;
        try {
            orderCommonData = orderService.list(pageInfo, orderStatusArray,intOrderId, start, end, intUserId, orderSn);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }

        return BaseRespVo.ok(orderCommonData);
    }

    @GetMapping("channel")
    public BaseRespVo channel() {
        List<CourierVO> list = null;
        try {
            list = orderService.channel();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }
        return BaseRespVo.ok(list);
    }

    @GetMapping("detail")
    public BaseRespVo detail(String id) {
        Integer intId=null;
        try {
            intId = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            return BaseRespVo.error(402,"订单id必须为数字");
        }
        OrderDetailVO orderDetail = null;
        try {
            orderDetail = orderService.detail(intId);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }
        return BaseRespVo.ok(orderDetail);
    }
    /**
     * 1. 获取请求参数--{"orderId":20,"refundMoney":395}
     * 2. 处理业务逻辑--退款,修改订单状态为已退款,更新退款时间
     * 3. 返回响应--成功
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/12 下午 09:08
     */
    @RequestMapping(value = "refund",method = RequestMethod.POST)
    public BaseRespVo refund(@RequestBody OrderRefundBO orderRefundBO, HttpServletRequest request){
        BigDecimal refundMoney = orderRefundBO.getRefundMoney();
        if(refundMoney.compareTo(new BigDecimal(0))!=1){
            return BaseRespVo.invalidParameter("退款金额必须为正数");
        }

        int code = 0;
        try {
            code = orderService.refund(orderRefundBO);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }
        if(code!=1){
            return BaseRespVo.invalidParameter("退款失败");
        }
        return BaseRespVo.ok();
    }


    /**
     * 1. 获取请求参数--{"orderId":539,"shipChannel":"ZTO","shipSn":"123"}
     * 2. 处理业务逻辑--修改订单状态的快递快递公司和快递单号,修改订单状态为已付款
     * 3. 返回响应--
     * @param orderShipBO
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/12 下午 10:05
     */
    @PostMapping("ship")
    public BaseRespVo ship(@RequestBody OrderShipBO orderShipBO,HttpServletRequest request){
        String shipChannel = orderShipBO.getShipChannel();
        if(StringUtil.isEmpty(shipChannel)){
            return BaseRespVo.invalidParameter("快递公司不能为空");
        }
        String shipSn = orderShipBO.getShipSn();
            if(StringUtil.isEmpty(shipSn)){
                return BaseRespVo.invalidParameter("快递编号不能为空");
            }
        int code= 0;
        try {
            code = orderService.ship(orderShipBO);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数值不对");
        }
        if(code!=1){
            return BaseRespVo.invalidParameter("退款失败");
        }
        return BaseRespVo.ok();
    }

    /**
     * 1. 获取请求参数--{"orderId":19}
     * 2. 处理业务逻辑--逻辑删除订单,修改逻辑删除为true
     * 3. 返回响应--{"errno":623,"errmsg":"订单不能删除"}
     * @return java44th.group.bean.admin.vo.BaseRespVo
     * @author 小焦
     * @since 2022/10/12 下午 10:21
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Map<String,Integer> map, HttpServletRequest request){
//        int id = Integer.parseInt(orderId);
        int code=orderService.delete(map.get("orderId"));
        if(code!=1){
            return BaseRespVo.error(623,"订单不能删除");
        }
        return BaseRespVo.ok();

    }

    /**
     * 1. 获取请求参数--{"commentId":1102,"content":"阿发送到发"}
     * 2. 处理业务逻辑--为商品增加回复信息
     * 3. 返回响应--ok
     * @param orderReplyBO
     * @return
     */
    @PostMapping("reply")
    public BaseRespVo reply(@RequestBody OrderReplyBO orderReplyBO){

        if (StringUtil.isEmpty(orderReplyBO.getContent())){
            return BaseRespVo.error(402, "回复内容不能为空");
        }
        int code= 0;
        try {
            code = orderService.reply(orderReplyBO);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.error(402, "参数有误");
        }
        if(code==622){
            return BaseRespVo.error(code, "订单商品已回复！");
        }
        if(code!=1){
            return BaseRespVo.invalidParameter("回复失败");
        }
        return BaseRespVo.ok();
    }


}
